//******************************************************************
//
// Copyright 2014 Intel Corporation.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include <stdlib.h>
#include <math.h>
#include "mraa.h"

#define ONBOARD_BUTTON_PIN 20

namespace Sensors
{
mraa_gpio_context btn_gpio = NULL;

inline void SetupPins()
{
    btn_gpio = mraa_gpio_init(ONBOARD_BUTTON_PIN); // Initialize pin 20
    if (btn_gpio != NULL)
    {
		cout << "good init" << endl;
        mraa_gpio_dir(btn_gpio, MRAA_GPIO_IN); // Set direction to INPUT
	}
    else
	{
		cout << "bad init" << endl;
	}
	
}

inline void ClosePins()
{
    mraa_gpio_close(btn_gpio);
}

inline int GetButtonState()
{
    if (btn_gpio == NULL)
    {
       	btn_gpio = mraa_gpio_init(ONBOARD_BUTTON_PIN); // Initialize pin 20
    	if (btn_gpio != NULL)
        	mraa_gpio_dir(btn_gpio, MRAA_GPIO_IN); // Set direction to INPUT
    }
    if (btn_gpio != NULL)
        return mraa_gpio_read(btn_gpio); // Reads from GPIO
    return -1;
}
}
